//
//  ChooseRoomTypeViewController.m
//  ZhuanZuBao
//
//  Created by 戴伟 on 2020/9/10.
//  Copyright © 2020 戴伟. All rights reserved.
//

#import "ChooseRoomTypeViewController.h"
#import "PickerViewCell.h"

@interface ChooseRoomTypeViewController ()<UIPickerViewDelegate, UIPickerViewDataSource>

@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (weak, nonatomic) IBOutlet UIView *contentView;

@end

@implementation ChooseRoomTypeViewController

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(0);
    }];
    
    [UIView animateWithDuration:0.4 animations:^{
        [self.view layoutIfNeeded];
        self.view.backgroundColor = [UIColor colorWithHexString:@"#000000" andAlpha:0.5];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = [UIColor colorWithHexString:@"#000000" andAlpha:0];
    [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(225);
    }];
}

- (IBAction)cancel:(id)sender {
    [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(225);
    }];
    
    [UIView animateWithDuration:0.4 animations:^{
        [self.view layoutIfNeeded];
        self.view.backgroundColor = [UIColor colorWithHexString:@"#000000" andAlpha:0];
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion: nil];
    }];
}

- (IBAction)confirm:(id)sender {
    NSInteger index1 = [_pickerView selectedRowInComponent:0];
    NSInteger index2 = [_pickerView selectedRowInComponent:1];
    NSInteger index3 = [_pickerView selectedRowInComponent:2];
    
    if (self.selectRoomTypeBlock) {
        self.selectRoomTypeBlock([NSString stringWithFormat:@"%ld室%ld厅%ld卫", index1+1, index2, index3]);
    }
    [self cancel:nil];
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return 10;
    }else if (component == 1) {
        return 4;
    }
    return 3;
}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 44;
}

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    PickerViewCell *cell = (PickerViewCell *)view;
    if (!cell) {
        cell = [[PickerViewCell alloc] initWithFrame:CGRectMake(0, 0, DEVICE_SCREEN_WIDTH, 44)];
    }
    if (component == 0) {
        cell.textLabel.text = [NSString stringWithFormat:@"%ld室", row+1];
    }else if(component == 1){
        cell.textLabel.text = [NSString stringWithFormat:@"%ld厅", row];
    }else{
        cell.textLabel.text = [NSString stringWithFormat:@"%ld卫", row];
    }
    return cell;
}


@end
